from rest_framework import serializers
from .models import Notification


class NotificationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Notification
        fields = ['id', 'recipient', 'sender', 'message', 'timestamp', 'is_read']
        read_only_fields = ['id', 'timestamp']  # 这些字段在创建通知时应为只读

    def create(self, validated_data):
        """
        创建一个新的通知。
        """
        return Notification.objects.create(**validated_data)
